package com.yanqu.road.server.logic.activity.condition.liveshow;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

public class LiveShowDayCondition extends BaseActivityCondition {

    public LiveShowDayCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.LiveShowDay.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.LiveShowDay.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        if(getValue().compareTo(activityConditionInfo.getParamList().get(0)) >= 0){//直播任务客户端写错了，将错就错服务端这边改成跟客户端一致
            return true;
        }
        return false;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        long dayLiveShowTime = (long) object;
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        BigInteger realValue = getValue();
        List<Long> recordTimeList = StringUtils.stringToLongList(getParam(), "\\|");
        long param1 = activityConditionInfo.getParamList().get(3).longValue();//直播任务客户端写错了，将错就错服务端这边改成跟客户端一致
        if(dayLiveShowTime >= param1 * DateHelper.MINUTE_MILLIONS){
            if(!recordTimeList.contains(zeroTime)){//加上天数
                realValue = realValue.add(BigInteger.ONE);
                recordTimeList.add(zeroTime);
                setParam(StringUtils.listToString(recordTimeList, "|"));
                setValue(realValue);
            }
        }
    }
}
